package com.ovolucky.mapmytrip.app.place;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Parse location details of a place from json response.
 */
public class PlaceDetailsJsonParser {
  /** Receives a JSONObject and returns a list */
  public static List<HashMap<String,String>> parse(JSONObject jsonObject){

    Double lat = Double.valueOf(0);
    Double lng = Double.valueOf(0);
    String formattedAddress = "";

    HashMap<String, String> hm = new HashMap<>();
    List<HashMap<String, String>> list = new ArrayList<>();

    try {
      lat = (Double)jsonObject.getJSONObject("result").getJSONObject("geometry").getJSONObject("location").get("lat");
      lng = (Double)jsonObject.getJSONObject("result").getJSONObject("geometry").getJSONObject("location").get("lng");
      formattedAddress = (String) jsonObject.getJSONObject("result").get("formatted_address");

    } catch (JSONException e) {
      e.printStackTrace();
    }catch(Exception e){
      e.printStackTrace();
    }

    hm.put("lat", Double.toString(lat));
    hm.put("lng", Double.toString(lng));
    hm.put("formatted_address",formattedAddress);

    list.add(hm);

    return list;
  }
}
